if not exists ( select  *
                from    sys.objects
                where   schema_name([schema_id]) = 'dab'
                        and name = 'SelectLkOrders'
                        and type = 'P' ) 
    exec ( 'create proc dab.SelectLkOrders as return null')
go

alter proc dab.SelectLkOrders
as        
    begin	
  --set nocount on
  
        select  cast(cast(o.blob as varbinary(max)) as xml) as Data
               ,o.Id
               ,o.Comment
               ,o.ordertype + '. ' + dt.name as Value
               ,a.name as UserName
               ,r.code UserId
               ,o.DateCreate
               ,o.date_create DateEdit
               ,o.Status_id
        from    [SRVSTA-TP01].DAB.dbo.lkOrder o with (nolock)
        join    [SRVSTA-TP01].DAB.dbo.Actor a with (nolock) on a.id = o.Actor_id
        join    [SRVSTA-TP01].DAB.dbo.ActorRoles r on  a.id = r.actor_id
        join    [SRVSTA-TP01].DAB.dbo.DocumsStatusType dt with (nolock) on o.status_id = dt.id
        where   o.Status_id not in (7, 8, 4, 1) 

    end
go

-- Test
if (1 = 1) 
    begin

        exec dab.SelectLkOrders

    end

/*

      */